package com.cai.quartz.schedule;

import com.cai.quartz.job.MyJob2;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.context.annotation.Configuration;

//@Configuration
public class MySchedule2 {
    public MySchedule2() throws SchedulerException {
        doSomeThing();
    }

    public void doSomeThing() throws SchedulerException {
        //1.调度器 从工厂中获取调度的实例
        Scheduler scheduler= StdSchedulerFactory.getDefaultScheduler();

        //2.任务实例 JobDetail
        JobDetail jobDetail = JobBuilder
                .newJob(MyJob2.class)
                // 任务名称  任务组名称
                .withIdentity("job1", "group1")
                .usingJobData("count",0)
                .build();

        //3.触发器
        Trigger trigger = TriggerBuilder
                .newTrigger()
                // 触发器名称  触发器组组名称
                .withIdentity("job1", "group1")
                //马上执行
                .startNow()
                //设定每五秒执行一次
                .withSchedule(
                        SimpleScheduleBuilder
                                .repeatSecondlyForever(5)
                )
                .build();

        //调度器管理任务和触发器，保证按照触发器定义的条件执行任务
        scheduler.scheduleJob(jobDetail,trigger);
        //启动
        scheduler.start();
    }
}
